package com.yssoft.controller.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yssoft.entity.core.TDepartment;
import com.yssoft.service.core.DepartmentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 陈翼东
 * @说明 部门管理
 * @time 2018/4/27 上午 10:12
 */
@Controller
@RequestMapping("department")
public class DepartmentController {
    
    @Resource
    private DepartmentService departmentService;
    
    /**
     * 跳转到我企业的部门页面
     * @return
     */
    @RequestMapping("departmentPage")
    public String departmentPage(Model model){
        model.addAttribute("json",JSONArray.toJSON(departmentService.queryMyDeptTableTree()));
        return "core/department/departmentList";
    }
    
    
    /**
     * 分页查询该企业的部门信息
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("queryDepartmentPage")
    @ResponseBody
    public JSONObject queryDepartmentPage(int page, int limit, String name, String code) {
        JSONObject jsonObject = departmentService.queryDepartmentPage(page, limit, name, code);
        return jsonObject;
    }
    
    /**
     * 多条删除部门信息
     * @return
     */
    @RequestMapping("deleteDepartment")
    @ResponseBody
    public Integer deleteDepartment(String id){
        return departmentService.deleteDepartment(id);
    }
    
    /**
     * 跳转到修改/添加页面
     * @param id 部门id
     * @return
     */
    @RequestMapping("editPage")
    public String editPage(String id,Model model){
        if(id!=null){
            TDepartment department =departmentService.queryTDepartmentById(Integer.parseInt(id));
            Integer parentDeptId = department.gettDId();//父级部门id
            TDepartment parentDept = departmentService.queryTDepartmentById(parentDeptId);
            
            model.addAttribute("department",department);
            model.addAttribute("parentDept",parentDept);
        }
        return "core/department/departmentEdit";
    }
    /**
     * 跳转到查看页面
     * @param id 部门id
     * @return
     */
    @RequestMapping("viewPage")
    public String viewPage(String id,Model model){
        TDepartment department =departmentService.queryTDepartmentById(Integer.parseInt(id));
        Integer parentDeptId = department.gettDId();//父级部门id
        TDepartment parentDept = departmentService.queryTDepartmentById(parentDeptId);
        model.addAttribute("department",department);
        model.addAttribute("parentDept",parentDept);
        return "core/department/departmentView";
    }
    
    /**
     * 保存部门信息
     * @param department
     * @return
     */
    @RequestMapping("saveDepartment")
    public String saveDepartment(TDepartment department){
        if(department.getId()!=null){
            departmentService.updateDepartment(department);
        }else {
            departmentService.addDepartment(department);
        }
        return "redirect:departmentPage";
    }
    
    /**
     * 查询企业中部门信息Tree页面
     *
     * @return
     */
    @RequestMapping("/queryMyDeptPage")
    public String queryMyDeptPage(Model model) {
        List<Map<String, Object>> jsonArray = departmentService.queryMyDeptTree();
        model.addAttribute("json", JSONArray.toJSON(jsonArray));
        return "core/person/deptTree";
    }
}
